﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using CoolCode.Services;
using System.Collections.Specialized;

namespace CoolCode.Web.Security
{
    public class LinqRoleProvider : RoleProvider, IRoleProvider
    {
        public static string DefaultProviderName = "LinqRoleProvider";
        public static string DefaultProviderDescription = "Linq Role Provider";

        private string _applicationName;

        public override string ApplicationName
        {
            get
            {
                return _applicationName;
            }
            set
            {
                _applicationName = value;
            }
        }

        private IRoleProvider _service;

        public IRoleProvider GetService()
        {
            if (_service == null)
            {
                _service = ServiceProvider.Get();
                _service.ApplicationName = this.ApplicationName;
            }
            return _service;
        }


        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            var service = GetService();
            service.AddUsersToRoles(usernames, roleNames);
        }

        public override void CreateRole(string roleName)
        {
            var service = GetService();
            service.CreateRole(roleName);
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            var service = GetService();
            return service.DeleteRole(roleName, throwOnPopulatedRole);
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var service = GetService();
            return service.FindUsersInRole(roleName, usernameToMatch);
        }

        public override string[] GetAllRoles()
        {
            var service = GetService();
            return service.GetAllRoles();
        }

        public override string[] GetRolesForUser(string username)
        {
            var service = GetService();
            return service.GetRolesForUser(username);
        }

        public override string[] GetUsersInRole(string roleName)
        {
            var service = GetService();
            return service.GetUsersInRole(roleName);
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var service = GetService();
            return service.IsUserInRole(username, roleName);
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            var service = GetService();
            service.RemoveUsersFromRoles(usernames, roleNames);
        }

        public override bool RoleExists(string roleName)
        {
            var service = GetService();
            return service.RoleExists(roleName);
        }

        #region - Initialize -

        /// <summary>
        /// Initializes the provider.
        /// </summary>
        /// <param name="name">The friendly name of the provider.</param>
        /// <param name="config">A collection of the name/value pairs representing the provider-specific attributes specified in the configuration for this provider.</param>
        /// <exception cref="T:System.ArgumentNullException">The name of the provider is null.</exception>
        /// <exception cref="T:System.InvalidOperationException">An attempt is made to call <see cref="M:System.Configuration.Provider.ProviderBase.Initialize(System.String,System.Collections.Specialized.NameValueCollection)"></see> on a provider after the provider has already been initialized.</exception>
        /// <exception cref="T:System.ArgumentException">The name of the provider has a length of zero.</exception>
        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
                throw new ArgumentNullException("config");
            if (string.IsNullOrEmpty(name))
            {
                name = DefaultProviderName;
            }
            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", DefaultProviderDescription);
            }

            // Initialize the abstract base class.
            base.Initialize(name, config);

            // initialize custom fields
            _applicationName = config.GetValue("applicationName", System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);

            ServiceProvider.Initial(config["connectionStringName"]);

        }
        #endregion
    }
}
